/*****************************************************************************
 * instance.c: Main instance functions for VLC Java Bindings
 *****************************************************************************
 * Copyright (C) 1998-2006 The VideoLAN team
 * Copyright (C) 2007 The LegendTV Project
 *
 * Authors: Filippo Carone <filippo@carone.org>
 *          Philippe Morin <phmorin@free.fr>
 * 			Guy Paddock <gap7472@rit.edu>
 *
 * $Id$
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include <jni.h>
#include <vlc/libvlc.h>

#include <stdlib.h> // for calloc

#include "../faults/jFaults.h"		// JNI fault tolerance
#include "../faults/memTrack.h"		// dynamic memory allocation tracking

#include "generated/VLCInstance.h"	// JVLC internal import, generated by gcjh
#include "exceptions.h"

static const char* MODULE_NAME						= "vlc";
static const char* INSTANCE_GET_ID_SIGNATURE		= "()J";
static const char* INTERFACE_GET_INSTANCE_SIGNATURE = "()Lorg/legendtv/core/VLCInstance;";

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
	JFault_init();
	MemTrack_start(MODULE_NAME);
	
	return JNI_VERSION_1_4;
}

JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *reserved)
{	
	MemTrack_stop(MODULE_NAME, 1);
	JFault_finalize();
}

libvlc_instance_t* __getInstance(JNIEnv *env, jobject vlcInstance)
{
	jclass		instanceClass;
	jmethodID	getIDMethod;
	jlong		retVal;
	
	instanceClass	= (*env)->GetObjectClass(env, vlcInstance);
	getIDMethod		= (*env)->GetMethodID(env, instanceClass, "getID", INSTANCE_GET_ID_SIGNATURE);
	retVal			= (*env)->CallLongMethod(env, vlcInstance, getIDMethod);
    
    return ((libvlc_instance_t*)retVal);
}

libvlc_instance_t* JVLC_getIntfInstance(JNIEnv *env, jobject intfInstance)
{
	jclass		intfClass;
    jmethodID	getInstMethod;
	jobject		vlcInstance;
	
	intfClass		= (*env)->GetObjectClass(env, intfInstance);	
    getInstMethod	= (*env)->GetMethodID(env, intfClass, "getInstance", INTERFACE_GET_INSTANCE_SIGNATURE);	
	vlcInstance		= (*env)->CallObjectMethod(env, intfInstance, getInstMethod);
	
	return (__getInstance(env, vlcInstance));
}

JNIEXPORT jlong JNICALL Java_com_googlecode_legendtv_core_VLCInstance_createInstance(JNIEnv *env, jobject _this, jobjectArray args)
{
	jlong	retVal;
	
	JFAULT_TOLERANT_SCOPE_WITH_RETURN(env,
	{
		int					argc;
		const char			**argv;
		libvlc_exception_t	*exception	= JVLC_initException();
		
		
		argc	= (int)((*env)->GetArrayLength(env, (jarray)args));
		argv	= (const char **)malloc(argc * sizeof(char*));
		
		for (int i = 0; i < argc; i++)
		{
			jstring	curElem;
			
			curElem	= (jstring)(*env)->GetObjectArrayElement(env, args, i);
			argv[i] = (*env)->GetStringUTFChars(env, curElem, 0);
		}
		
		retVal	= (long)libvlc_new(argc, (char**)argv, exception);
		
		CHECK_EXCEPTION_FREE;
	}, retVal);
	
	return retVal;
}

JNIEXPORT void JNICALL Java_com_googlecode_legendtv_core_VLCInstance_destroy(JNIEnv *env, jobject _this) 
{
	JFAULT_TOLERANT_SCOPE(env,
	{
		libvlc_instance_t* instance	= __getInstance(env, _this);
		
		libvlc_destroy(instance, NULL);
	});
}
